package dataconn;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class  Dataconn{
 ResultSet rs = null;
 Connection conn=null;
 Statement stmt=null;
 //连接池
 public  static  synchronized Connection getConnection() throws Exception
 {
  try
  {
   
   Context initCtx = new javax.naming.InitialContext(); 
            Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
            DataSource ds = (DataSource)envCtx.lookup("jdbc/gc"); ;
   return ds.getConnection();
  }
  catch(SQLException e)
  {
   throw e;
  }
  catch(NamingException e)
  {
       throw e;
  }
  
 }
 //更新数据
 public void executeUpdate(String sql) {
  try {
   conn = getConnection();
   }
  catch (Exception ex) {
                           ex.printStackTrace();
                   }
  try {
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
   stmt.executeUpdate(sql);
   } 
  catch(SQLException ex) { 
   System.err.println("aq.executeUpdate: " + ex.getMessage());
   System.err.println("aq.executeUpadatestrSQL: " + sql);
   }
  }
  
 //查询数据
 public ResultSet executeQuery(String sql) {
   rs = null;
   try {
   conn = getConnection();
   }
  catch (Exception ex) {
                           ex.printStackTrace();
                   }
   try {
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    rs = stmt.executeQuery(sql);
    } 
   catch(SQLException ex) { 
    System.err.println("aq.executeQuery: " + ex.getMessage());
    System.err.println("aq.executeQuerystrSQL: " + sql);
   }
   return rs;
  }  
  
 public void closeStmt()
  {
   try{
    if(stmt!=null){
     stmt.close();
    }
      }
   catch(SQLException e){
    e.printStackTrace();
      }
  }


 public void closeConn()
 {
  try{
   if (conn!=null){
    conn.close();
   }
     }
  catch(SQLException e){
   e.printStackTrace();
     }
 }
}


 